<template>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门编号" prop="number">
                        <el-input v-model="form.number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="所属公司" prop="companyId">
                        <el-select v-model="form.companyId" clearable placeholder="请选择：">
                            <el-option v-for="item in configData.companyList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="上级部门" prop="parentId">
                        <el-tree-select v-model="form.parentId" :data="parentData" :render-after-expand="false"
                            :check-strictly=true />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="联系电话" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门分类" prop="deptType">
                        <el-select v-model="form.deptType">
                            <el-option v-for="item in configData.deptTypeList" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="负责人" prop="leader">
                        <sc-table-select v-model="leaderRows" :apiObj="apiObj" :fileName="fileName" :table-width="700"
                            :props="tableProps" :defaultSelected="form.leader" @change="change">
                        </sc-table-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="顺序号" prop="itemIndex">
                        <el-input-number v-model="form.itemIndex" :min="0" />
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" size="large" inline-prompt active-value="normal"
                            inactive-value="disable" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-form-item label="传真" prop="fax">
                        <el-input v-model="form.fax"></el-input>
                    </el-form-item>
                </el-col>

            </el-row>
            
        </el-form>
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="updataDept(formRef)">确 定</el-button>
        </div>
</template>


<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from 'element-plus';
import Api from '@/api/model/user'
import sysApi from '@/api/model/system'
const emit = defineEmits(['updatetable'])
const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
onMounted(() => {
  getData();
  getParent();
});

const form = ref({});
const formRef = ref();
const leaderRows = ref({})
const apiObj = ref(sysApi.select.query)
const fileName = ref('ChooseEmployeeReport')
const tableProps = {
    label: 'name',
    value: 'id',
}
const formRules = reactive({
  number: [{ required: true, message: "请输入编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});

const updateVisible = (val) => {
  // visible.value = false
  emit('updatetable')
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

const configData = ref({});

const parentData = ref([]);
const getParent = async () => {
  let params = {
    fileName: "DeptTreeReport",
  };
  let res = await Api.dept.DeptTree.post(params);
  parentData.value = res.reportData;
  
};
const change = (val) =>{
    form.value.leaderId = val.id
    form.value.leader = val.name
}
const getData = async () => {
  let params = {
    id:props.paramsData.id
  }
  let res = await Api.dept.detail.post(params);
  if (res.code == 200) {
    form.value = res.data;
    configData.value = res.configData;
  }
};

const updataDept = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updataDeptData();
    }
  });
};

const updataDeptData = async () => {
  var params = {
    bean: form.value,
  };
  var res = await Api.dept.update.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "修改成功！",
      duration: 2000,
    });
    emit("updatetable",true);
  }
};
</script>

<style lang="less" scoped></style>

